<template>
  <div class="base-content system-sampleDeliveryInfo-content">
    <div class="basic-info">
      <el-form ref="filterForm" :model="filterForm" inline>
        <el-form-item label="委托单号" prop="orderNum">
          <el-input
            v-model="filterForm.orderNum"
            placeholder="委托单号"
            clearable
          />
        </el-form-item>
        <el-form-item
          v-if="activeName == 'first'"
          label="委托单位"
          prop="customerName"
        >
          <el-input
            v-model="filterForm.customerName"
            placeholder="委托单位"
            clearable
          />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button
            v-loading="searchLoading"
            type="primary"
            @click="getList('filter')"
            >查询</el-button
          >
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>
      <!--操作栏-->
      <div class="base-content-operate">
        <el-button
          v-rights="'Add'"
          icon="el-icon-plus"
          type="primary"
          @click="addSample"
          >新增样品</el-button
        >
        <el-button
          v-rights="'Delete'"
          icon="el-icon-delete"
          type="danger"
          :disabled="!submitLoad"
          @click="deleteSampleRegistration"
          >删除</el-button
        >
        <el-button
          v-rights="'Export'"
          v-loading="uploadLoad"
          icon="el-icon-document"
          type="primary"
          @click="exportData"
          >导出</el-button
        >
        <el-button
          v-if="activeName == 'second'"
          v-rights="'CopySample'"
          icon="el-icon-document-copy"
          type="primary"
          @click="copySample"
          >复制样品</el-button
        >
        <!-- <el-button icon="el-icon-document-copy" type="primary">导入</el-button> -->
      </div>
      <el-main class="roller_main">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane v-if="moduleEnableEntrust" label="待办" name="first">
            <SampleCommission
              v-if="activeName == 'first'"
              ref="SampleCommission"
              :total.sync="total"
              :module-enable-entrust="moduleEnableEntrust"
              :pagination.sync="pagination"
              :filter-form.sync="filterForm"
            />
          </el-tab-pane>
          <el-tab-pane label="已办" name="second">
            <SampleFinished
              v-if="activeName == 'second'"
              ref="SampleFinished"
              :total.sync="total"
              :module-enable-entrust="moduleEnableEntrust"
              :pagination.sync="pagination"
              :filter-form.sync="filterForm"
            />
          </el-tab-pane>
        </el-tabs>
      </el-main>
      <!--分页-->
      <BasePagination
        :total="total"
        :current-page="pagination.pageIndex"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>
    <SampleRegistrationDialog
      v-if="dialogFormVisible"
      ref="SampleRegistrationDialog"
      :dialog-form-visible.sync="dialogFormVisible"
      :sample-registration-id.sync="sampleRegistrationId"
      :module-enable-entrust="moduleEnableEntrust"
      :edit-id.sync="editId"
      :entrust-id.sync="entrustId"
      :sample-detection-id.sync="sampleDetectionId"
      :state.sync="state"
      @close="closeDialog"
    />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import {
  SampleWord,
  DownloadExportWordZip,
} from "../../../api/SampleManage/Export";
import { deleteRecord } from "../../../api/SampleManage/SampleRegistration";
import SampleCommission from "./SampleCommission.vue";
import SampleFinished from "./SampleFinished.vue";
import SampleRegistrationDialog from "./SampleRegistrationDialog.vue";
import { getList as getListInit } from "../../../api/EntrustManage/EntrustSampleDetection.js";
import _ from "lodash";
export default {
  name: "SampleRegistrationList",
  components: { SampleCommission, SampleRegistrationDialog, SampleFinished },
  data() {
    return {
      // 样品登记id
      sampleRegistrationId: "",
      state: false,
      // 样品检测id
      sampleDetectionId: "",
      // 委托单id
      entrustId: "",
      uploadLoad: false,
      searchLoading: false,
      loading: false,
      filterForm: {
        orderNum: "",
        customerName: "",
        symbol: "=",
        sampleBusinessStatusCode: 9,
        entrustOrderId: 0,
      },
      dialogFormVisible: false,
      editId: "",
      activeName: "first",
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      total: 1,
      submitLoad: true,
    };
  },
  computed: {
    ...mapGetters(["moduleEnableEntrust"]),
  },
  created() {
    if (
      this.$route.query !== null &&
      this.$route.query.entrustOrderId !== undefined &&
      this.$route.query.entrustOrderId !== null &&
      this.$route.query.entrustOrderId !== 0
    ) {
      this.filterForm.entrustOrderId = this.$route.query.entrustOrderId;
    }
    if (this.moduleEnableEntrust) {
      this.activeName = "first";
    } else {
      this.activeName = "second";
    }
    // 初始化数据
    if (this.$route.query.orderNum !== undefined) {
      this.pagination.pageIndex = 1;
      this.filterForm.orderNum = this.$route.query.orderNum;
      this.filterForm.entrustSampleStatusCode = 0;
      this.filterForm.finishedState = "0";
      getListInit(this.filterForm, this.pagination)
        .then((res) => {
          if (res.data.dataList != null) {
            this.activeName = "first";
          } else {
            this.activeName = "second";
          }
        })
        .catch((res) => {});
    }
  },
  mounted() {},
  methods: {
    changePage(page) {
      this.pagination.pageIndex = page;
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
    },
    // 导出
    exportData() {
      if (this.activeName !== "second") {
        this.$message({
          message: "请选择已办数据",
          type: "warning",
        });
        return false;
      }
      // if (this.$refs.SampleFinished.tableDatas.length > 1) {
      //   this.$message({
      //     message: "请选择一条数据",
      //     type: "warning",
      //   });
      //   return false;
      // }
      this.uploadLoad = true;
      var idList = [];
      for (let i = 0; i < this.$refs.SampleFinished.tableDatas.length; i++) {
        idList.push(this.$refs.SampleFinished.tableDatas[i].id);
      }
      SampleWord({ id: idList, exportWordTypeEnum: 2 })
        .then((res) => {
          DownloadExportWordZip({ path: res.data, exportWordTypeEnum: 2 })
            .then((res) => {
              this.uploadLoad = false;
              var blob = new Blob([res.data], {
                type: "application/octet-stream",
              });
              var url = window.URL.createObjectURL(blob);
              const link = document.createElement("a"); // 创建a标签
              link.href = url;
              link.download = "产品质量记录单.zip"; // 重命名文件
              link.click();
              URL.revokeObjectURL(url); // 释放内存
            })
            .catch(() => {
              this.uploadLoad = false;
            });
        })
        .catch(() => {
          this.uploadLoad = false;
        });
    },
    // 删除样品登记
    deleteSampleRegistration: _.throttle(function() {
      if (this.submitLoad) {
        this.submitLoad = false;
        if (this.activeName === "first") {
          this.$message({
            message: "请选择已办数据",
            type: "warning",
          });
          this.submitLoad = true;
          return false;
        }
        var idList = [];
        for (let i = 0; i < this.$refs.SampleFinished.tableDatas.length; i++) {
          idList.push(this.$refs.SampleFinished.tableDatas[i].id);
        }

        deleteRecord(idList)
          .then((res) => {
            this.submitLoad = true;

            this.getList();
          })
          .catch((res) => {
            this.submitLoad = true;
          });
      }
    }, 2000),
    cancelForm() {
      this.filterForm.entrustOrderId = 0;
      this.$refs.filterForm.resetFields();
      this.getList();
    },
    // 关闭弹框触发事件
    closeDialog() {
      this.entrustId = "";
      this.sampleDetectionId = "";
      this.sampleRegistrationId = "";
      this.getList();
    },
    getList() {
      if (this.activeName === "first") {
        this.pagination.pageIndex = 1;
        this.filterForm.entrustSampleStatusCode = 0;
        this.filterForm.finishedState = "0";
        this.$refs.SampleCommission.getList();
      } else {
        this.pagination.pageIndex = 1;
        this.filterForm.sampleBusinessStatusCode = 10;
        this.filterForm.symbol = ">=";
        this.filterForm.finishedState = "1";
        this.$refs.SampleFinished.getList();
      }
    },
    // 复制样品
    copySample() {
      var data = [];
      if (this.activeName === "first") {
        data = this.$refs.SampleCommission.tableDatas;
      } else if (this.activeName === "second") {
        data = this.$refs.SampleFinished.tableDatas;
      }
      if (data === null || data.length === 0) {
        this.$message({
          message: "请选择数据",
          type: "warning",
        });
        return false;
      }
      if (data.length > 1) {
        this.$message({
          message: "只能选择一条数据",
          type: "warning",
        });
        return false;
      }

      if (this.activeName === "first") {
        this.entrustId = data[0].entrustOrderId;
        this.sampleDetectionId = data[0].id;
      } else if (this.activeName === "second") {
        this.sampleRegistrationId = data[0].id;
      }
      this.state = true;
      this.dialogFormVisible = true;
    },
    // 新增样品
    addSample() {
      this.state = false;
      this.dialogFormVisible = true;
    },
    // 选择标签
    handleClick() {
      this.pagination = {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      };
      if (this.activeName === "first") {
        this.filterForm.entrustSampleStatusCode = 0;
        this.filterForm.finishedState = "0";
      } else {
        this.filterForm.sampleBusinessStatusCode = 10;
        this.filterForm.symbol = ">=";
        this.filterForm.finishedState = "1";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.system-sampleDeliveryInfo-content {
  padding: 0;
  background: #f8fbff;
  display: flex;
  align-items: flex-start;

  .sampleDeliveryInfo-content {
    width: calc(100%);
    height: 100%;
    background: #fff;
    padding: 28px 20px 0;
    box-sizing: border-box;
    position: relative;
    .el-table {
      margin-top: 12px;
    }
  }
}
</style>
